package com.shell.governor.modules.util;

import com.shell.bee.base.annotation.Keyword;
import com.shell.bee.base.annotation.KeywordMap;
import com.shell.bee.base.exception.UnCheckException;
import com.shell.bee.base.utils.StringUtil;
import com.shell.governor.modules.message.keyword.SysSndApplyKeyword;
import com.shell.governor.modules.po.PubMessageTemplate;
import com.shell.governor.modules.vo.KeywordVo;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MessageTemplateHelper {
	
	private static String prefix = "{";
	private static String suffix = "}";

	public static String genMsgContent(PubMessageTemplate messageTemplate, Object params) throws UnCheckException {
		Map<String, String> replaceKeyword = parseParams(params);
		Set<String> keys = replaceKeyword.keySet();
		String msgContent = messageTemplate.getContent();
		for (String key : keys) {
			if (replaceKeyword.get(key) != null) {
				msgContent = msgContent.replaceAll("\\" + prefix + key + "\\" + suffix,
						(String) replaceKeyword.get(key));
			}
		}
		return msgContent;
	}

	public static String genMsgTitle(String content, Object params) throws UnCheckException {
		if (StringUtil.emptyAndNull(content)) {
			return "";
		}
		Map<String, String> replaceKeyword = parseParams(params);
		Set<String> keys = replaceKeyword.keySet();
		for (String key : keys) {
			if (replaceKeyword.get(key) != null) {
				content = content.replaceAll("\\" + prefix + key + "\\" + suffix, (String) replaceKeyword.get(key));
			}
		}
		return content;
	}

	public static Map<String, String> parseParams(Object params) throws UnCheckException {
		return parseParams(params, params.getClass());
	}

	private static Map<String, String> parseParams(Object params, Class clazz) throws UnCheckException {
		Map<String, String> m = new HashMap();
		if (null == clazz.getAnnotation(KeywordMap.class)) {
			return m;
		}
		Field[] inFields = clazz.getDeclaredFields();
		Field.setAccessible(inFields, true);
		try {
			for (Field f : inFields) {
				Object annotation = f.getAnnotation(Keyword.class);
				if (null != annotation) {
					m.put(f.getName(), (String) f.get(params));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Class superClazz = clazz.getSuperclass();
		Object pm = parseParams(params, superClazz);
		((Map) pm).putAll(m);
		return (Map<String, String>) pm;
	}

	public static List<KeywordVo> getKeyword(Class clazz) throws UnCheckException {
		List<KeywordVo> keywords = new ArrayList();
		if (null == clazz.getAnnotation(KeywordMap.class)) {
			return keywords;
		}
		Field[] inFields = clazz.getDeclaredFields();
		Field.setAccessible(inFields, true);
		for (Field f : inFields) {
			Object annotation = f.getAnnotation(Keyword.class);
			if (null != annotation) {
				KeywordVo keywordVo = new KeywordVo();
				Keyword keyword = (Keyword) annotation;
				keywordVo.setCode(prefix + f.getName() + suffix);
				keywordVo.setName(keyword.name());
				keywordVo.setPreview(keyword.preview());
				keywords.add(keywordVo);
			}
		}
		Class superClazz = clazz.getSuperclass();
		keywords.addAll(getKeyword(superClazz));
		return keywords;
	}

	public static void main(String[] args) {
		SysSndApplyKeyword kw = new SysSndApplyKeyword();

		kw.setSellerNm("ddd");
		kw.setSndDate("12345678");
		kw.setBillAmt("12");
		kw.setUserName("������");
		try {
			String str = "����������������������{userName}��{sndDate}������������������������{sellerNm}   ������#{billAmt}������������������������������";
			PubMessageTemplate t = new PubMessageTemplate();
			t.setContent(str);
			String m = genMsgContent(t, kw);
			System.out.println(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
